﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZhuZhongWei1.Models;

namespace ZhuZhongWei1.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 获取分组
        /// </summary>
        /// <returns></returns>
        public ActionResult GetTypeId()
        {
            using (CEntities1 db = new CEntities1())
            {
                var list = db.video_type.Select(p => new { type_id = p.type_id, type_name = p.type_name }).ToList();
                return Json(list, JsonRequestBehavior.AllowGet);
            }
        }

        /// <summary>
        /// 多条件加分页查询
        /// </summary>
        /// <returns></returns>
        public ActionResult PostTable(int page, int limit, string video_name, string state, string type_id)
        {
            using (CEntities1 db = new CEntities1())
            {//这里也是if判断
                var datalist = db.video_move.Include("video_type").AsEnumerable();
                if (!String.IsNullOrEmpty(video_name))
                {
                    datalist = datalist.Where(p => p.video_name.Contains(video_name));
                }
                if (!String.IsNullOrEmpty(state))
                {
                    datalist = datalist.Where(p => p.state == state);
                }
                if (!String.IsNullOrEmpty(type_id))
                {
                    datalist = datalist.Where(p => p.type_id == Convert.ToInt32(type_id));
                }
                var list = datalist.Select(p => new
                {
                    ///调取的这些参数传递给表格
                    video_id = p.video_id,
                    video_name = p.video_name,
                    type_id = p.type_id,
                    type_Name = p.video_type.type_name,
                    play_url = p.play_url,
                    introduce = p.introduce,
                    state = p.state,
                    year = p.year
                });
                var count = list.Count();//总数
                return Json(new { count = count, data = list.OrderByDescending(p => p.video_id).Skip((page - 1) * limit).Take(limit).ToList() });
                //json数据
            }
        }

        /// <summary>
        /// 添加方法
        /// </summary>
        /// <param name="video"></param>
        /// <returns></returns>
        public ActionResult PostAddForm(video_move video)
        {
            using (CEntities1 db = new CEntities1())
            {
                db.video_move.Add(video);
                var zhi = db.SaveChanges();
                if (zhi > 0)
                {
                    return Json(new { code = true, msg = "添加成功" });//也是json先用if判断当值丹玉0就返回添加成功
                }
                return Json(new { code = false, msg = "添加失败" });//不大于0就是返回添加失败
            }
        }
    }
}